package com.example.qx.lianxra;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.qx.R;
import com.example.qx.adapter.xiaoxiadapter;
import com.example.qx.database.data;
import com.example.qx.lei.itemxiaoxi;
import com.example.qx.lei.leilianxr;
import com.example.qx.lei.liaotianrenheneirong;
import com.example.qx.lei.zhangmi;

import org.w3c.dom.Text;

import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class liaotian extends AppCompatActivity {
 private Socket socket;
 private EditText neirong;
 private Button fasong;
 private  zhangmi zhangmi= com.example.qx.lei.zhangmi.getInstance();
 private  data data=new data();
 private itemxiaoxi itemxiaoxi= com.example.qx.lei.itemxiaoxi.instance();
 private RecyclerView recyclerView;
private TextView textView;

    public liaotian() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.liaotian);
        textView = findViewById(R.id.liantianmingzi);
        fasong = findViewById(R.id.fasongaaaaa);
        neirong = findViewById(R.id.fasongneirong);
        recyclerView = findViewById(R.id.lixixiaoxi);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        Bundle bundle = getIntent().getExtras();
        final xiaoxiadapter[] xiaoxiadapter = new xiaoxiadapter[1];
        try {
            xiaoxiadapter[0] = new xiaoxiadapter(data.getxiaoxijilu( zhangmi.getZhang(), bundle.getInt("zhanghao")), this);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        recyclerView.setAdapter(xiaoxiadapter[0]);

        final Connection[] connection = new Connection[1];
        final leilianxr[] leilianxr = new leilianxr[1];
        final leilianxr[] leilianxr1 = new leilianxr[1];
        CountDownLatch latch=new CountDownLatch(1);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {Class.forName("com.mysql.jdbc.Driver").newInstance();}
                catch (IllegalAccessException | ClassNotFoundException |
                       InstantiationException e) {
                    throw new RuntimeException(e);}
                try {
                    connection[0] = DriverManager.getConnection("jdbc:mysql://sh-cdb-9761pjk2.sql.tencentcdb.com:29081/youqi","root","asd123456");
                    leilianxr[0] = data.getgrxx(bundle.getInt("zhanghao"),connection[0]);
                    leilianxr1[0] = data.getgrxx(zhangmi.getZhang(), connection[0]);
                } catch (SQLException e) {throw new RuntimeException(e);}
                latch.countDown();
            }
        }).start();
        try{
            latch.await();
            connection[0].close();
        }
        catch (InterruptedException | SQLException e)
        {e.printStackTrace();}


        textView.setText(leilianxr[0].getMingzi());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle1=new Bundle();
                bundle1.putInt("duifangzhanghao",bundle.getInt("zhanghao"));
                Intent intent =new Intent(liaotian.this,xiaoxiguanli.class);
                 intent.putExtras(bundle1);
                startActivity(intent);
            }
        });

        fasong.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onClick(View v) {
                String a = neirong.getText().toString();
                if (!a.equals("")) {
                    List<xiaoxijilu> llll = null;
                    try {
                        llll = data.getxiaoxijilu( zhangmi.getZhang(), bundle.getInt("zhanghao"));
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    int sum;
                    if (llll.size() == 0)
                        sum = 1;
                    else
                    { sum = llll.get(llll.size() - 1).getSum();
                    sum = sum + 1;}
                    try {
                        data.insertxiaoxijilu( zhangmi.getZhang(), bundle.getInt("zhanghao"), sum, a, leilianxr1[0].getTouxiang());
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    xiaoxiadapter[0].notifyDataSetChanged();
                    try {
                        xiaoxiadapter[0] = new xiaoxiadapter(data.getxiaoxijilu( zhangmi.getZhang(), bundle.getInt("zhanghao")), getApplication());
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    recyclerView.setAdapter(xiaoxiadapter[0]);
                    neirong.setText("");
                }

            }
        });

    }

}